﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardMixForms
{
    public partial class StandardMixMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.ToolBll toolsBll = new BLL.ToolBll();
        BLL.StandardMixBll standardMixBll = new BLL.StandardMixBll();
        public StandardMixMainForm()
        {
            InitializeComponent();
        }

        private void StandardMixMainForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            //绑定grid里的样品名称
            var goodsList = toolsBll.GetAllGoodList();
            repositoryItemLookUpEdit1.DataSource = goodsList;

            //绑定 grid里的 检验项目
            var testItemList = toolsBll.GetAllTestItemList();
            repositoryItemLookUpEdit2.DataSource = testItemList;

            InitGrid();
        }

        private void InitGrid()
        {
            var models = standardMixBll.LoadEntities(m => true).OrderByDescending(m => m.StandardMixID).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            StandardMixDetailForm form = new StandardMixDetailForm(InitGrid);
            form.Show();
        }

        private void barLargeButtonItem_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ShowEditForm();
        }

        private void ShowEditForm()
        {
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("StandardMixID"));
            var standardMix = standardMixBll.LoadEntities(m => m.StandardMixID == id).First();
            StandardMixDetailForm form = new StandardMixDetailForm(standardMix);
            form.Show();
        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("\n确定删除本条数据吗?   \n\n\n    确认是否退出(O/C)", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("StandardMixID"));
                var standardMix = standardMixBll.LoadEntities(m => m.StandardMixID == id).First();

                bool res = standardMixBll.DeleteEntity(standardMix);
                if (res)
                {
                    InitGrid();
                    XtraMessageBox.Show("删除成功！");
                }

            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            ShowEditForm();
        }

   

       
    }
}